package com.platform.modules.sys.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.Constant;
import com.platform.common.utils.Query;
import com.platform.modules.sys.dao.SysUserTokenDao;
import com.platform.modules.sys.entity.SysUserTokenEntity;
import com.platform.modules.sys.oauth2.TokenGenerator;
import com.platform.modules.sys.service.SysUserTokenService;

/**
 * @author 肖亮
 */
@Service("sysUserTokenService")
public class SysUserTokenServiceImpl extends ServiceImpl<SysUserTokenDao, SysUserTokenEntity>
		implements SysUserTokenService {

	@Override
	@Deprecated
	public String createToken(String userId) {
		// 生成一个token
		String token = TokenGenerator.generateValue();

		// 当前时间
		Date now = new Date();
		// 过期时间
		Date expireTime = new Date(now.getTime() + Constant.EXPIRE * 1000);

		// 判断是否生成过token
		SysUserTokenEntity tokenEntity = this.getById(userId);
		if (tokenEntity == null) {
			tokenEntity = new SysUserTokenEntity();
			tokenEntity.setUserId(userId);
			tokenEntity.setToken(token);
			tokenEntity.setUpdateTime(now);
			tokenEntity.setExpireTime(expireTime);

			// 保存token
			this.save(tokenEntity);
		} else {
			tokenEntity.setToken(token);
			tokenEntity.setUpdateTime(now);
			tokenEntity.setExpireTime(expireTime);

			// 更新token
			this.updateById(tokenEntity);
		}

		return token;
	}


	@Override
	public String createToken(String userId,String clientId) {
		// 生成一个token
		String token = TokenGenerator.generateValue();

		// 当前时间
		Date now = new Date();
		// 过期时间
		Date expireTime = new Date(now.getTime() + Constant.EXPIRE * 1000);

		// 判断是否生成过token
		//SysUserTokenEntity tokenEntity = this.getById(userId);
        SysUserTokenEntity tokenEntity = this.baseMapper.queryByUserIdAndClientId(userId, clientId);
		if (tokenEntity == null) {
			tokenEntity = new SysUserTokenEntity();
			tokenEntity.setToken(token);
			tokenEntity.setUserId(userId);
			tokenEntity.setClientId(clientId);
			tokenEntity.setUpdateTime(now);
			tokenEntity.setExpireTime(expireTime);

			// 保存token
			this.save(tokenEntity);
		} else {
			tokenEntity.setToken(token);
			tokenEntity.setUpdateTime(now);
			tokenEntity.setExpireTime(expireTime);

			// 更新token
			this.updateById(tokenEntity);
		}

		return token;
	}

	@Override
	public void logout(String userId, String token) {
		SysUserTokenEntity tokenEntity = baseMapper.queryByToken(token);
		if(null != tokenEntity) {
			//查询token对象没有userId 或 userId与入参不符
			if(null==tokenEntity.getUserId() || !tokenEntity.getUserId().equals(userId)) {
				throw new BusinessException("token与用户不匹配");
			}else {
				// 生成一个token
				String tokenNew = TokenGenerator.generateValue();
				tokenEntity.setToken(tokenNew);
				this.updateById(tokenEntity);
			}
		}else {
			throw new BusinessException("token无效");
		}
	}

	@Override
	public SysUserTokenEntity queryOne(Map<String, Object> params) {
		List<SysUserTokenEntity> list =  this.baseMapper.selectList(params);
		if(list!=null && list.size()==1){
			return list.get(0);
		}else{
			return null;
		}
	}

	@Override
	public List<SysUserTokenEntity> queryList(Map<String, Object> params) {
		List<SysUserTokenEntity> list =  this.baseMapper.selectList(params);
		return list;
	}

	@Override
	public Page<SysUserTokenEntity> queryPage(Map<String, Object> params) {
		Page<SysUserTokenEntity> page = new Query<SysUserTokenEntity>(params).getPage();
		params.put("nowDate", new Date());
		return page.setRecords(baseMapper.selectList(page, params));
	}

	@Override
	@Deprecated
	@Transactional(rollbackFor = Exception.class)
	public void delete(String userId) {
		this.removeById(userId);
	}

	@Override
	@Deprecated
	@Transactional(rollbackFor = Exception.class)
	public void offlineBatch(String[] userIds) {
		this.removeByIds(Arrays.asList(userIds));
	}


}
